package com.cy.law.home.service.impl;

import com.cy.law.db.dao.*;
import com.cy.law.db.entity.Human;
import com.cy.law.db.entity.Report;
import com.cy.law.db.entity.Unit;
import com.cy.law.home.service.HomeService;
import com.cy.law.utils.SessionUtil;
import com.cy.law.utils.SimpleSpecification;
import com.cy.law.vo.PageInfo;
import com.cy.law.vo.RequestResult;
import com.cy.law.vo.Tree;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class HomeServiceImpl implements HomeService {
    @Autowired
    DicGroupDao dicGroupDao;
    @Autowired
    NavItemDao navItemDao;
    @Autowired
    HumanNavItemDao humanNavItemDao;
    @Autowired
    HumanDao humanDao;
    @Autowired
    UnitDao unitDao;
    @Autowired
    ReportDao reportDao;

    @Override
    public RequestResult getHumanNavItem(HttpServletRequest request) {
        RequestResult result = RequestResult.new0();
        Integer humanId = SessionUtil.getHumanId(request);
        if (humanId != null) {
            result.setMapData("group", dicGroupDao.findHumanDicGroupInfo(humanId));
            result.addMapData("item", humanNavItemDao.findHumanNavItems(humanId));
            return result;
        } else {
            return RequestResult.new1();
        }
    }

    @Override
    public RequestResult getHumanInfoById(int humanId) {
        Human human = humanDao.findById(humanId).orElse(null);
        if (human == null) {
            return RequestResult.new1().setMessage("无法查询到相关人员,人员标识:" + humanId);
        } else {
            if (human.getUnitId() != null && human.getUnitId() > 0) {
                human.setUnitName(unitDao.findUnitNameById(human.getUnitId()));
            }
            return RequestResult.new0().setMapData("human", human);
        }
    }

    @Override
    public List<Tree> getHumanLevelTree(int unitId) {
        List<Tree> myTree = new ArrayList<>();
        Tree tree = new Tree(1, "宁波市执法局");
        myTree.add(tree);
        tree.setIconCls("icon-unit");
        //第一次加载，直接加载三级，以后每次加载一级
        List<Human> humans = humanDao.findAllByIsLawPerson(1);
        List<Unit> units = unitDao.findAll();
        if (unitId <= 0) {
            findSubUnitHumanTree(tree, unitId, 1, 2, units, humans);
        } else {
            findSubUnitHumanTree(tree, unitId, 1, 1, units, humans);
        }
        return myTree;
    }

    @Override
    public RequestResult getReportList(String deviceId, int currentPage, int numPerPage) {
        Pageable pageable = PageRequest.of(currentPage - 1, numPerPage, Sort.unsorted());
        Page<Report> page = reportDao.findAll(new SimpleSpecification<>(new HashMap<String, Object>() {{
            put("deviceId", deviceId);
        }}, null), pageable);
        RequestResult result = RequestResult.new0();
        result.setMapData("pageInfo", PageInfo.Page2PageInfo(page));
        result.addMapData("list", page.getContent());
        return result;
    }

    /**
     * 递归查找子树
     *
     * @param tree     树
     * @param unitId   部门标志
     * @param curLevel 当前层级，以1开始
     * @param maxLevel 最大层级，从1开始
     */
    private void findSubUnitHumanTree(Tree tree, int unitId, int curLevel, int maxLevel,
                                      List<Unit> allUnitList, List<Human> allHumanList) {
        List<Tree> trees = tree.getChildren();
        List<Human> humanList = new ArrayList<>();
        if (allHumanList != null && allHumanList.size() > 0) {
            for (Human human : allHumanList) {
                if (human.getUnitId() != null && Objects.equals(human.getUnitId(), unitId)) {
                    humanList.add(human);
                }
            }
        }
        if (humanList.size() > 0) {
            for (Human human : humanList) {
                Tree t = new Tree();
                t.setText(human.getHumanName());
                t.setIconCls("icon-law");
                t.setId(human.getUnitId());
                trees.add(t);
            }
        }
        List<Unit> unitList = new ArrayList<>();
        if (allUnitList != null && allUnitList.size() > 0) {
            for (Unit unit : allUnitList) {
                if (unit.getSeniorId() != null && Objects.equals(unit.getSeniorId(), unitId)) {
                    unitList.add(unit);
                }
            }
        }
        if (unitList.size() > 0) {
            for (Unit unit : unitList) {
                Tree t = new Tree();
                t.setText(unit.getUnitName());
                t.setId(unit.getUnitId());
                t.setIconCls("icon-unit");
                findSubUnitHumanTree(t, unit.getUnitId(), curLevel + 1, maxLevel,
                        allUnitList, allHumanList);
                trees.add(t);
                if (curLevel >= 1 && t.getChildren().size() > 0) {
                    t.setState("closed");
                }
            }
        }
    }
}
